package com.hxm.drawview;

import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.LinearInterpolator;
import android.widget.Scroller;

/**
 * Created by hxm on 2017/11/11.
 * 描述：
 */
public class HScrollViewGroup extends ViewGroup {

    private Scroller scroller;
    private int screenHeight;
    private int downY;
    private int startScrollY;
    private boolean isMoving = false;
    private int slop;

    public HScrollViewGroup(Context context) {
        this(context, null);
    }

    public HScrollViewGroup(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HScrollViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        screenHeight = dm.heightPixels;
        scroller = new Scroller(context, new LinearInterpolator());
        slop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            View childView = getChildAt(i);
            measureChild(childView, widthMeasureSpec, heightMeasureSpec);
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int count = getChildCount();
        //设置ViewGroup的高度
        LayoutParams params = getLayoutParams();
        params.height = screenHeight * count;
        setLayoutParams(params);
        for (int i = 0; i < count; i++) {
            View childView = getChildAt(i);
            if (childView.getVisibility() != GONE) {
                childView.layout(l, screenHeight * i, r, screenHeight * (i + 1));
            }
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int y = (int) event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downY = (int) event.getY();
                startScrollY = getScrollY();
                break;
            case MotionEvent.ACTION_MOVE:
                if (!scroller.isFinished()) {
                    scroller.abortAnimation();
                }
                int dy = downY - y;
                isMoving = dy > slop;
                Log.i("dy", dy + "");
                Log.i("downY", downY + "");
                Log.i("getScrollY", getScrollY() + "");
                if (getScrollY() < -300) {
                    dy = 0;
                }
                if (getScrollY() > getHeight() - screenHeight + 300) {
                    dy = 0;
                }
                scrollBy(0, dy);
                downY = y;
                break;
            case MotionEvent.ACTION_UP:
//                if (!isMoving){
//                    if (getScrollY()<0){
//                        scroller.startScroll(0,getScrollY(),0,0);
//                    }
//                    if (getScrollY()>getHeight()-screenHeight){
//                        scroller.startScroll(0,getScrollY(),0,getHeight()-screenHeight);
//                    }
//                }
                int endScrollY = getScrollY();
                int dScrollY = endScrollY - startScrollY;
//                if (getScrollY()<0||getScrollY()>getHeight()-screenHeight){
                if (dScrollY > 0) {
                    //向上滑
                    if (dScrollY > screenHeight / 3) {
                        scroller.startScroll(0, getScrollY(), 0, screenHeight - dScrollY);
                    } else {
                        scroller.startScroll(0, getScrollY(), 0, -dScrollY);
                    }
                } else {
                    //向下滑
                    if (-dScrollY > screenHeight / 3) {
                        scroller.startScroll(0, getScrollY(), 0, -screenHeight - dScrollY);
                    } else {
                        scroller.startScroll(0, getScrollY(), 0, -dScrollY);
                    }
                }
//                }

                break;
        }
        postInvalidate();
        return true;
    }

    @Override
    public void computeScroll() {
        super.computeScroll();
        if (scroller.computeScrollOffset()) {
            scrollTo(0, scroller.getCurrY());
            postInvalidate();
        }
    }
}
